﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace SPREST.Models
{
    public class List : IXmlSerializable
    {
        public List()
        {

        }

        public string ID { get; set; }
        public string Title { get; set; }
        public SPListItemCollection ListItems { get; set; }


        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            //don't provide a schema
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            //TODO:
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("Fields", "");

            foreach (SPListItem item in ListItems)
            {
                foreach (SPField field in item.Fields)
                {
                    if (item[field.Id] != null)
                    {
                        writer.WriteElementString("Field", item[field.Id].ToString());

                        //writer.WriteAttributeString("Field", field.Title);
                    }
                }
            }
            writer.WriteEndElement();
        }

        #endregion


        public static List<List> GetAllLists()
        {
            List<List> allLists = new List<List>();

            using (SPSite site = new SPSite(ServerConnection.GetServerNameFromConfig()))
            {
                foreach (SPWeb web in site.AllWebs)
                {
                    foreach (SPList list in web.Lists)
                    {
                        allLists.Add(list.Convert());
                    }
                }
            }
            return allLists;
        }

        public static List<List> GetListsForSite(string id)
        {
            List<List> allLists = new List<List>();

            using (SPSite site = new SPSite(ServerConnection.GetServerNameFromConfig()))
            {
                foreach (SPWeb web in site.AllWebs)
                {
                    foreach (SPList list in web.Lists)
                    {
                        if (web.ID.ToString() == id)
                        {
                            allLists.Add(list.Convert());
                        }
                    }
                }
            }
            return allLists;
        }

        public static List GetSingleList(string id)
        {
            //TODO: change the implementation                        
            using (SPSite site = new SPSite(ServerConnection.GetServerNameFromConfig()))
            {
                foreach (SPWeb web in site.AllWebs)
                {
                    foreach (SPList list in web.Lists)
                    {
                        if (list.ID.ToString() == id)
                        {
                            return list.Convert();
                        }
                    }
                }
            }

            return null;
        }
    }
}
